package com.core.cloudbusiness.system.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.core.cloudbusiness.system.user.service.CloudSystemUserService;

import com.core.cloudcommon.CommonPageResponse;
import com.core.cloudcommon.CommonResult;
import com.core.cloundbusinessapi.system.user.dto.CloudSystemUserDTO;
import com.core.cloundbusinessapi.system.user.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author =====Lqj=====
 * @className CloudSystemUserController
 * @description 系统用户控制层
 * @create 2024/11/13 21:28
 **/
@RestController
@RequestMapping("/cloud/sys/user")
public class CloudSystemUserController {

    @Autowired
    private CloudSystemUserService cloudSystemUserService;

    @PostMapping("/add")
    public CommonResult<CloudSystemUserDTO> add(@Validated @RequestBody CloudSystemAddUserVO cloudSystemUserVO) {
        CloudSystemUserDTO add = cloudSystemUserService.add(cloudSystemUserVO);
        return CommonResult.OK(add);
    }

    /**
     * 分页查询
     *
     * @param params
     * @return
     */
    @PostMapping("/findPage")
    public CommonResult<CommonPageResponse> findPage(@Validated @RequestBody CloudSystemQueryListUserVO params) {
        CommonPageResponse result = cloudSystemUserService.findPage(params);
        return CommonResult.OK(result);
    }

    /**
     * 根据ID查询
     *
     * @return
     */
    @PostMapping("/findById")
    public CommonResult<CloudSystemUserDTO> findById(@Validated @RequestBody CloudSystemAddUserVO params) {
        CloudSystemUserDTO CloudSystemUserDTO = cloudSystemUserService.findById(params.getId());
        return CommonResult.OK(CloudSystemUserDTO);
    }


    /**
     * 修改
     *
     * @param CloudSystemUserDTO
     * @return
     */
    @PostMapping("/update")
    public CommonResult<Integer> update(@Validated @RequestBody CloudSystemQueryListUserVO CloudSystemUserDTO) {
        Integer result = cloudSystemUserService.update(CloudSystemUserDTO);
        return CommonResult.OK(result);
    }

    /**
     * 根据ID删除
     *
     * @return
     */
    @PostMapping("/{id}")
    public CommonResult<Integer> delete(@Validated @RequestBody CloudSystemAddUserVO CloudSystemUserDTO) {
        int result = cloudSystemUserService.delete(CloudSystemUserDTO.getId());
        return CommonResult.OK(result);
    }


    /**
     * 用户注册
     */
    @PostMapping("/register")
    public CommonResult<CloudSystemUserDTO> register(@Validated @RequestBody CloudSystemRegisterVO vo) {
        CloudSystemUserDTO user = cloudSystemUserService.register(vo);
        return CommonResult.OK(user);
    }

    /**
     * 修改密码（需要前端从token解析或服务端上下文拿到userId）
     */
    @PostMapping("/changePassword")
    public CommonResult<Boolean> changePassword(@RequestHeader("userId") Long userId,
                                                @Validated @RequestBody CloudSystemChangePasswordVO vo) {
        boolean ok = cloudSystemUserService.changePassword(userId, vo);
        return CommonResult.OK(ok);
    }

    /**
     * 重置密码（短信验证码）
     */
    @PostMapping("/resetPassword")
    public CommonResult<Boolean> resetPassword(@Validated @RequestBody CloudSystemResetPasswordVO vo) {
        boolean ok = cloudSystemUserService.resetPassword(vo);
        return CommonResult.OK(ok);
    }

    /**
     * 获取个人资料
     */
    @GetMapping("/profile")
    public CommonResult<CloudSystemUserDTO> getProfile(@RequestHeader("userId") Long userId) {
        return CommonResult.OK(cloudSystemUserService.getProfile(userId));
    }

    /**
     * 更新个人资料
     */
    @PostMapping("/profile")
    public CommonResult<CloudSystemUserDTO> updateProfile(@RequestHeader("userId") Long userId,
                                                          @Validated @RequestBody CloudSystemUpdateProfileVO vo) {
        return CommonResult.OK(cloudSystemUserService.updateProfile(userId, vo));
    }
}
